package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingProjectSexrviceImpl implements NursingProjectService {


    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 分页条件查询护理项目
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse selectByPage(String name, Integer status, Integer pageNum, Integer pageSize) {

        //设置分页
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingProjectVo> nursingProjects = nursingProjectMapper.selectByPage(name, status);
        return PageResponse.of(nursingProjects,NursingProjectVo.class);
    }

    @Override
    public void insert(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto,nursingProject);
        nursingProjectMapper.insert(nursingProject);
    }

    @Override
    public void deleteById(Long id) {
        nursingProjectMapper.deleteById(id);
    }

    @Override
    public NursingProjectVo findById(Long id) {
        NursingProjectVo nursingProjectVo = new NursingProjectVo();
        NursingProject byId = nursingProjectMapper.findById(id);
        BeanUtils.copyProperties(byId,nursingProjectVo);
        return nursingProjectVo;
    }

    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto,nursingProject);
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        if(status==1){
            status=0;
        }else {
            status=1;
        }
        NursingProject nursingProject = new NursingProject();
        nursingProject.setStatus(status);
        nursingProject.setId(id);
        nursingProjectMapper.updateStatus(id,status);
    }

    @Override
    public List<NursingProjectVo> findAll() {
        List<NursingProjectVo> all = nursingProjectMapper.findAll();
        for (NursingProjectVo nursingProjectVo : all) {
            if(nursingProjectVo.getCount()==null){
                nursingProjectVo.setCount(0);
            }
        }
        return all;
    }
}
